package com.jjh.dao.impl;

import com.jjh.consts.Properties;
import com.jjh.pojo.User;
import com.jjh.util.FileUtils;
import com.jjh.winterframework.annatation.Component;
import com.jjh.dao.UserDao;
import com.jjh.wrapper.QueryBody;
import com.jjh.wrapper.QueryWrapper;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserDaoImpl implements UserDao {

    private final static String userPath = "E:\\EeleMa\\src\\main\\resources\\users.txt";

    @Override
    public String getAddressById(int id) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("Id",String.valueOf(id));


        List<Object> objects = FileUtils.pageQueryList(userPath, User.class, wrapper);

        if(objects.size()==0){
            throw new IllegalArgumentException("无此id的用户");
        }else {
            User user = (User) objects.get(0);
            return user.getAddress();
        }

    }

    /**
     * 根据条件获取用户实体
     * @param wrapper
     * @return
     */
    @Override
    public User getOne(QueryWrapper wrapper) {

        List<Object> objects = FileUtils.pageQueryList(Properties.USER_PATH, User.class, wrapper);

        User user = (User) objects.get(0);
        return user;


    }

    /**
     * 根据主键修改用户
     * @param user 用户
     * @return
     */
    @Override
    public boolean updateOne(User user) {

     return   FileUtils.updateOne(Properties.USER_PATH,user);
    }


}
